package com.sumapay.fsb.common.util;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import javax.servlet.ServletRequest;
import java.awt.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class CommonUtil {

    public final static char[] ELEMENTS = {'1','2','3','4','5','6','7','8','9',
            'a','b','c','d','e','f','g','h','i',
            'j','k','m','n','p','q','r','s','t',
            'u','v','w','x','y','z','A','B','C',
            'D','E','F','G','H','I','J','K','L',
            'M','N','P','Q','R','S','T','U','V',
            'W','X','Y','Z'};

    public static MultiValueMap<String, Object> processRequestParameter(ServletRequest servletRequest) {
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
        servletRequest.getParameterMap().forEach((key, value) -> {
            map.put(key, Collections.singletonList(value[0]));
        });
        return map;
    }

    public static Map<String, Object> paramConvertToSourceMap(ServletRequest servletRequest) {
        Map<String, Object> map = new HashMap<>();
        servletRequest.getParameterMap().forEach((key, value) -> {
            map.put(key, value[0]);
        });
        return map;
    }
    /*
     * 给定范围获得随机颜色
     */
    public static Color getRandColor(int fc, int bc){
        Random random = new Random();
        if(fc>255) fc=255;
        if(bc>255) bc=255;
        int r=fc+random.nextInt(bc-fc);
        int g=fc+random.nextInt(bc-fc);
        int b=fc+random.nextInt(bc-fc);

        return new Color(r,g,b);
    }


    public static String randomCode() {
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }
}
